package com.iknow.net.translate.impl;

import com.iknow.IKnow;
import com.iknow.IKnowApiV4;
import com.iknow.data.Word;
import com.iknow.data.Word.SentInfo;
import com.iknow.file.CacheSystem;
import com.iknow.http.HttpException;
import com.iknow.http.Response;
import com.iknow.http.ResponseException;
import com.iknow.net.translate.IKTranslateManager.IKTranslate;
import com.iknow.util.DomXmlUtil;
import com.iknow.util.StringUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class IKIcibaTranslate
  implements IKTranslateManager.IKTranslate
{
  private static final String url = "http://dict-co.iciba.com/api/dictionary.php?w=";
  private Map<String, Word> cache = new HashMap();

  private String getUrl(String paramString, boolean paramBoolean)
  {
    return "http://dict-co.iciba.com/api/dictionary.php?w=" + paramString;
  }

  private Element request(String paramString)
  {
    Element localElement1;
    Element localElement2;
    try
    {
      localElement2 = IKnow.mApi.get(paramString, false).asDocument().getDocumentElement();
      localElement1 = localElement2;
      return localElement1;
    }
    catch (ResponseException localResponseException)
    {
      localResponseException.printStackTrace();
      localElement1 = null;
    }
    catch (HttpException localHttpException)
    {
      while (true)
        localHttpException.printStackTrace();
    }
  }

  private Element requestLocal(String paramString)
  {
    Object localObject;
    String str;
    try
    {
      str = IKnow.mCacheSystem.readFile(paramString);
      if (str == null)
      {
        localObject = null;
      }
      else
      {
        Response localResponse = new Response();
        localResponse.setResponseAsString(str);
        Element localElement = localResponse.asDocument().getDocumentElement();
        localObject = localElement;
      }
    }
    catch (ResponseException localResponseException)
    {
      localResponseException.printStackTrace();
      localObject = null;
    }
    return localObject;
  }

  public void fillTranslate(Element paramElement, Word paramWord)
  {
    paramWord.setUserId("1");
    paramWord.setKey(DomXmlUtil.getTagItemValue(paramElement, "key"));
    paramWord.setLang(DomXmlUtil.getTagItemValue(paramElement, "pos"));
    paramWord.setDef(DomXmlUtil.getTagItemValue(paramElement, "acceptation"));
    paramWord.setPron(DomXmlUtil.getTagItemValue(paramElement, "ps"));
    paramWord.setAudioUrl(DomXmlUtil.getTagItemValue(paramElement, "pron"));
    List localList = paramWord.getSentList();
    NodeList localNodeList = paramElement.getElementsByTagName("sent");
    int i = 0;
    while (true)
    {
      if (i >= localNodeList.getLength())
        return;
      Node localNode = localNodeList.item(i);
      Word.SentInfo localSentInfo = new Word.SentInfo();
      localSentInfo.setOrig(DomXmlUtil.getTagItemValue(localNode, "orig"));
      localSentInfo.setAudioUrl(DomXmlUtil.getTagItemValue(localNode, "pron"));
      localSentInfo.setTrans(DomXmlUtil.getTagItemValue(localNode, "trans"));
      localList.add(localSentInfo);
      ++i;
    }
  }

  public Word translate(String paramString, boolean paramBoolean)
  {
    Object localObject2;
    Object localObject1 = new Word();
    if (StringUtil.isEmpty(paramString))
    {
      localObject2 = localObject1;
      return localObject2;
    }
    Word localWord = (Word)this.cache.get(paramString);
    if (localWord == null)
    {
      Element localElement = request(getUrl(paramString, false));
      if (localElement != null)
      {
        fillTranslate(localElement, (Word)localObject1);
        ((Word)localObject1).setKey(paramString);
      }
      if (!(StringUtil.isEmpty(((Word)localObject1).getKey())))
        this.cache.put(paramString, localObject1);
      ((Word)localObject1).setKey(paramString);
    }
    while (true)
    {
      while (true)
        localObject2 = localObject1;
      localObject1 = localWord;
    }
  }

  public Word translateLocal(String paramString, boolean paramBoolean)
  {
    Object localObject2;
    Object localObject1 = new Word();
    if (StringUtil.isEmpty(paramString))
    {
      localObject2 = localObject1;
      return localObject2;
    }
    Word localWord = (Word)this.cache.get(paramString);
    if (localWord == null)
    {
      Element localElement = requestLocal(getUrl(paramString, false));
      if (localElement != null)
      {
        fillTranslate(localElement, (Word)localObject1);
        ((Word)localObject1).setKey(paramString);
      }
      if (!(StringUtil.isEmpty(((Word)localObject1).getKey())))
        this.cache.put(paramString, localObject1);
    }
    while (true)
    {
      while (true)
        localObject2 = localObject1;
      localObject1 = localWord;
    }
  }
}